/*
 * @author zzr
 * @date: 2025/10/12  20:41
 * @description: 将两个升序链表合并为一个新的升序链表并返回
 * 新链表是通过拼接给定的两个链表的所有节点组成的
 */


public class Demo8 {

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    public ListNode mergeTwoLists(ListNode headA, ListNode headB) {

        ListNode newHead = new ListNode();
        ListNode tmpHead = newHead;

        while (headA != null && headB != null) {
            if (headA.val > headB.val) {
                tmpHead.next = headB;
                headB = headB.next;
            } else {
                tmpHead.next = headA;
                headA = headA.next;
            }
            tmpHead = tmpHead.next;
        }
        if (headA != null) {
            tmpHead.next = headA;
        }
        if (headB != null) {
            tmpHead.next = headB;
        }
        return newHead.next;
    }
}
